<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/6/20
 * Time: 11:18
 */

namespace app\common\controller;


use think\Controller;

class ApiSign extends Controller
{
    //获取签名key
    public function getsignkey($appid)
    {

        $Agent = new \app\common\model\Agent();
        $signkey = $Agent->where(array('sign_appid' => $appid))->value('sign_key');
        return $signkey;
    }

    //参数排序ASCII
    public function uni_array($arr)
    {
        foreach ($arr as $k => $v) {
            if (!$v)
                unset($arr[$k]);
        }
        return $arr;
    }

    //获取签名结果
    public function getsign($arrdata, $method = "sha1", $appid = "")
    {


        try {
            if (!function_exists($method)) return false;
            ksort($arrdata);
            $paramstring = "";
            foreach($arrdata as $key=>$value){
                if(isset($value) && !empty($value)){
                    $str[]=$key."=".$value;
                }
            }
            $paramstring=join("&",$str);
            if (!empty($appid)) {
                $signkey = $this->getsignkey($appid);
            } else {
                $signkey = $this->getsignkey($arrdata['appid']);
            }
            $Sign = $method($paramstring . "&key=" . $signkey);

            return $Sign;
        } catch (\Exception $e) {
            return $e;
        }
    }

}